﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RookieSharpLibs.Security;
using System.IO;

namespace RookieGen
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private const string MSG_TITLE = "鸟哥私人办公助理";
        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.Text = string.Format("注册机({0})",MSG_TITLE);
            string[] ArrName = GetSoftwareNames();
            this.cbbSftName.DataSource = ArrName;
            this.Icon = RookieGen.Properties.Resources.main;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGenKey_Click(object sender, EventArgs e)
        {
            //SoftNameDef softName = (SoftNameDef)Enum.Parse(typeof(SoftNameDef), this.cbbSftName.Text);
            string softName = this.cbbSftName.Text;
            SoftwareRegister softReg = new SoftwareRegister(softName);
            if (this.tbHostIdentify.Text == "")
            {
                MessageBox.Show("填写机器码", MSG_TITLE);
                return;
            }
            if (this.tbUserID.Text == "")
            {
                MessageBox.Show("填写账户", MSG_TITLE);
                return;
            }
            string str = softReg.GetRegStr(this.tbHostIdentify.Text, this.tbUserID.Text);
            this.rtbRegStr.Text = str;
            softReg = null;
        }

        private void MenuItemCopy_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(this.rtbRegStr.Text);
            }
            catch
            {

            }
        }
        private const string FILE_NAME = "sftname.txt";
        private string[] GetSoftwareNames()
        {
            using(FileStream FS = new FileStream(FILE_NAME,FileMode.OpenOrCreate,FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(FS))
                {
                    List<string> LstName = new List<string>();
                    string str;
                    while (null != (str = sr.ReadLine()))
                    {
                        str = str.Replace(" ", "");
                        if (str == "")
                        {
                            continue;
                        }
                        LstName.Add(str);
                    }
                    sr.Close();
                    FS.Close();
                    return LstName.ToArray();
                }
            }
        }
    }
}
